package com.qf.controller;

import com.alibaba.fastjson.JSON;
import com.qf.entity.Order;
import com.qf.entity.WeiXinPay;
import com.qf.service.OrderService;
import com.qf.service.impl.OrderServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * @author 10202
 * @version V1.0
 * @Project xiaomi
 * @Package ${PACKAGE_NAME}
 * @Date 2023/4/17 10:05
 */
@WebServlet(name = "PaySuccessServlet",urlPatterns = "/paySuccess")
public class PaySuccessServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        try {
            String json = request.getParameter("result");
            System.out.println(json);

            WeiXinPay pay = JSON.parseObject(json, WeiXinPay.class);

            String oid = pay.getResult().getOutTradeNo();

            if(pay.getResult().getResultCode().equalsIgnoreCase("success")){
                OrderService orderService=new OrderServiceImpl();
               Order order=orderService.queryOrderByOid(oid);
               order.setStatus("2");
               orderService.updateOrderByOrder(order);
                request.setAttribute("msg","支付成功");
            }else {
                request.setAttribute("msg","支付失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
            request.setAttribute("msg","支付失败"+e.getMessage());
        }
        request.getRequestDispatcher("/message.jsp").forward(request,response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }
}
